from util.font import get_font_maps


import requests
from util.getheaders import get_init_headers
import re

res = requests.get(f'https://piaofang.maoyan.com/dashboard-ajax/movie?orderType=0&uuid=19a38c693d4c8-078751b7567c018-26061b51-144000-19a38c693d5c8&timeStamp=1763446187205&User-Agent=TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzE0Mi4wLjAuMCBTYWZhcmkvNTM3LjM2&index=882&channelId=40009&sVersion=2&signKey=4ef0d7143d1ddce921d8bc2ba0b79b23&WuKongReady=h5', headers=get_init_headers())


# print(res.json()['fontStyle'])

r = re.search(r'\),url\("(.*?)"\);}', res.json()['fontStyle'])
if r:
    font_url = f"https:{r.group(1)}"
    font_res = requests.get(font_url)
    font_path = './static/fonts/maoyan.woff'
    with open(f'{font_path}', "wb") as f:
        f.write(font_res.content)


    # 字符映射表
    font_maps = get_font_maps(font_path)




def parse_field(value):
    items = []
    for item in value.split(";")[:-1]:
        print(item)
        if item.startswith("."):
            items.append(".")
            print(items)
            items.append(item[1:])
            print(items)
        else:
            items.append(item)

    result = ""
    for item in items:
        if item == ".":
            result += "."
        else:
            code = int(item[3:], base=16)
            if code in font_maps:
                result += font_maps[code]
                print(code, "映射结果", font_maps[code])
    return result




for movie in res.json()['movieList']['list']:
    print(parse_field(movie['boxSplitUnit']['num']), movie['movieInfo']['movieName'])
    print(movie['boxSplitUnit']['num'])
print(res.json()['movieList']['nationBoxInfo']['nationSplitBoxSplitUnit']['num'])
print(res.json()['movieList']['nationBoxInfo']['title'], parse_field(res.json()['movieList']['nationBoxInfo']['nationSplitBoxSplitUnit']['num']))


